package com.sihai.ehr.service.impl;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.SalaryStatistics;
import com.sihai.ehr.service.SalaryStatisticService;

/**
 * 员工薪资统计服务实现
 * 
 * @author henry
 * 
 */
@Service("salaryStatisticService")
public class SalaryStatisticServiceImpl implements SalaryStatisticService {

	@Resource
	private DataAccessor dataAccessor;

	@Override
	public SalaryStatistics get(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.get(SalaryStatistics.class, id);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取薪资统计明细数据库发生异常】【异常说明：+"
					+ e.getMessage()
					+ "】【发生地点：SalaryStatisticServiceImpl.get;参数id=" + id + " 】");
		}
	}

	@Override
	public SalaryStatistics get(final Long workId, final int year,
			final int month) {
		if (workId == null || workId < 0 || year < 0 || month < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findUnique(SalaryStatistics.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							Query query = session
									.createQuery("from SalaryStatistics s where s.employeeinfoWork.id=:workId and s.year=:year and s.month=:month");
							query.setParameter("workId", workId);
							query.setParameter("year", year);
							query.setParameter("month", month);
							return query;
						}
					});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取薪资统计明细数据库发生异常】【异常说明：+"
					+ e.getMessage()
					+ "】【发生地点：SalaryStatisticServiceImpl.get;参数workid="
					+ workId + " 】");
		}
	}

	@Override
	public LimitedList<SalaryStatistics> getSalaryStatisticByWorkId(
			final Long workId, Limit limit, Sorter sorter) {
		if (workId == null || workId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(SalaryStatistics.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from SalaryStatistics s where 1 <> 2");
							dynamicQuery.appendIfNotNull(
									"and s.workId.id =:workId", "workId",
									workId);
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：根据员工任职编号获取员工薪资统计信息列表时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：SalaryStatisticServiceImpl.getSalaryStatisticByWorkId;参数workId="
							+ workId + "】");
		}
	}

	@Override
	public void save(SalaryStatistics salaryStatistics) {
		if (salaryStatistics == null
				|| salaryStatistics.getEmployeeinfoWork() == null) {
			throw new RuntimeException("【salaryStatistics对象参数异常】");
		}
		try {
			dataAccessor.saveOrUpdate(salaryStatistics);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保存统计信息对象到数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：SalaryStatisticServiceImpl.save】");
		}
	}

	@Override
	public void deleteSalaryStatistics(final String ids) {
		if (ids == null || ids.isEmpty()) {
			throw new RuntimeException("【参数异常：ids为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from SalaryStatistics s where s.id in ({0})",
									ids));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：删除统计信息数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：SalaryStatisticServiceImpl.deleteSalaryStatistics】");
		}
	}

	@Override
	public void deleteSalaryStatisticDetails(final Long statisticId) {
		if (statisticId == null && statisticId <= 0) {
			throw new RuntimeException("【参数异常：statisticId参数错误】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from SalaryStatisticsDetail s where s.salaryStatistics.id={0}",
									statisticId));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：删除薪资统计项目细节数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：SalaryStatisticServiceImpl.deleteSalaryStatisticDetails】");
		}
	}

	@Override
	public LimitedList<SalaryStatistics> find(final int year, final int month,
			final Long organizationId, final Long dutyId, Long workId,
			Limit limit, Sorter sorter) {
		if (organizationId == null || organizationId < 0 || dutyId == null
				|| dutyId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(SalaryStatistics.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from SalaryStatistics s where 1 <> 2");
							dynamicQuery
									.appendIfNotNull(
											"and s.employeeinfoWork.organization.id =:organizationId",
											"organizationId", organizationId);
							dynamicQuery.appendIfNotNull(
									"and s.employeeinfoWork.duty.id =:dutyId",
									"dutyId", dutyId);
							if (year > 0) {
								dynamicQuery.appendIfNotNull(
										"and s.year =:year", "year", year);
							}
							if (month > 0) {
								dynamicQuery.appendIfNotNull(
										"and s.month =:month", "month", month);
							}
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：查询薪资统计结果信息时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：SalaryStatisticServiceImpl.find;参数organizationId="
							+ organizationId + ",dutyId=" + dutyId + "】");
		}
	}

}
